/*===========================================================================================================================
	Wolf Prop Protection
		By Goluch
			DO NOT UPLOAD TO GARRYSMOD.ORG
			
			
	Name: WPP.Menu
	Purpose: Creates the WPP Menu.
	Author: Goluch
===========================================================================================================================*/
function WPP.Menu()
	if ( WPP._Menu ) then
		WPP._Menu:Remove();
		return;
	end
	
	local NameSpace = 200;
	local Wide = (#WPP.Actions * 20) + NameSpace;
	local Tall = 160;
	local Gradient = surface.GetTextureID( "vgui/gradient_down" );

	local Frame = vgui.Create("DFrame");
	Frame:SetTitle("Wolf Prop Protection!");
	Frame:SetDraggable(true);
	Frame:SetSizable(false);
	Frame:SetDeleteOnClose(true);
	Frame:SetMouseInputEnabled( true );
	Frame:SetWide(Wide + 10);
	
	local Clean = vgui.Create("DButton",Frame);
	Clean:SetPos(5,30);
	Clean:SetSize(NameSpace - 10,20);
	Clean:SetText("Clean up");
	
	function Clean:DoClick()
		RunConsoleCommand("wpp","cleanup");
	end
	
	function Clean:Paint()
		surface.SetDrawColor( 255,255,255,255 );
		surface.DrawOutlinedRect( 0, 0, self:GetWide() , self:GetTall() );
	end
	
	local Clear = vgui.Create("DButton",Frame);
	Clear:SetPos(5,55);
	Clear:SetSize(NameSpace - 10,20);
	Clear:SetText("Wipe settings");
	
	function Clear:DoClick()
		RunConsoleCommand("wpp","wipe");
	end
	
	function Clear:Paint()
		surface.SetDrawColor( 255,255,255,255 );
		surface.DrawOutlinedRect( 0, 0, self:GetWide() , self:GetTall() );
	end
	
	local HudLabel = vgui.Create("DLabel",Frame);
	HudLabel:SetPos(5,80);
	HudLabel:SetText( "Hud Mode:" );
	HudLabel:SizeToContents();
	
	local HUD = vgui.Create( "DMultiChoice", Frame);
	HUD:SetPos(5,105);
	HUD:SetWide(NameSpace - 10);
	for String,_ in pairs ( WPP.HUDModes ) do
		HUD:AddChoice(String);
	end
	
	HUD:ChooseOption(WPP.HUDCvar:GetString() || "Error");
	function HUD:OnSelect(Value,String)
		RunConsoleCommand("wpp_hudmode",String);
	end
	
	local Globals = vgui.Create("WPPTickBar",Frame);
	Globals:SetPos(5,Tall);
	Globals:SetWide(Wide);
	
	Players = { };
	
	Frame.Think = function()
		for _,Player in pairs( player.GetAll() ) do
			if ( !Players[Player] ) and ( Player != LocalPlayer() ) then
				Players[Player] = vgui.Create("WPPTickBar",Frame);
				Players[Player]:SetPlayer(Player);
				Players[Player]:SetWide(Wide);
			end
			
		end
		
		for Player,Bar in pairs( Players ) do
			if ( !Player ) or ( !Player:IsValid() ) or ( !Player:IsPlayer() ) then
				Players[Player] = nil;
				Bar:Remove();
			end
			
		end
		
		local Y = Tall + 20;
		for Player,Bar in pairs( Players ) do
			Bar:SetPos(5,Y);
			Y = Y + 20;
		end
		
		Frame:SetTall( Tall + (#player.GetAll() * 20) );
	end
	
	function Frame:Close()
		WPP._Menu = nil
		self:Remove()
	end
	
	function Frame:Paint()
		surface.SetDrawColor( 0, 0, 0, 200 );
		surface.SetTexture( Gradient );
		surface.DrawTexturedRectRotated( self:GetWide()/2, 12, self:GetWide() , 22 , 180);
		
		surface.SetDrawColor( 0, 0, 0, 200 );
		surface.DrawRect( 0, 22, self:GetWide() , self:GetTall() - 22 );
		
		surface.SetDrawColor( 255,255,255,255 );
		surface.DrawOutlinedRect( 0, 0, self:GetWide() , self:GetTall() );
		
		local X = self:GetWide() - 5 - (#WPP.Actions * 20);
		surface.DrawLine(5,Tall - 20,X,Tall - 20);
		draw.SimpleText("Player List:","DefaultSmall",7,Tall - 10,Color(255,255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_CENTER);
		
		local Shade = true;
		for Index,Action in ipairs(WPP.Actions) do
			surface.SetDrawColor( 100,100,100,50 );
			if ( !Shade ) then
				surface.SetDrawColor( 100,100,100,100 );
			end
			surface.DrawRect(X,23,20,self:GetTall() - 30);
			Shade = !Shade;
			
			surface.SetDrawColor(255,255,255,200);
			surface.DrawLine(X,23,X,self:GetTall() - 5);
			local Y = 30;
			Action = string.ToTable( Action );
			for _,Letter in ipairs( Action ) do
				draw.SimpleText(Letter,"DefaultSmall",X + 10,Y,Color(255,255,255),TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER);
				Y = Y + 12;
			end
			
			X = X + 20;
		end
		
		surface.SetDrawColor(255,255,255,200);
		surface.DrawLine(X,23,X,self:GetTall() - 5);
			
		local Y = Tall - 20;
		local Shade = true;
		while ( Y < (self:GetTall() - 5) ) do
			surface.SetDrawColor( 100,100,100,50 );
			if ( !Shade ) then
				surface.SetDrawColor( 100,100,100,100 );
			end
			surface.DrawRect(5,Y,self:GetWide() - 10,20);
			Shade = !Shade;
			Y = Y + 20;
		end
		
	end
	
	
	Frame:Center();
	Frame:MakePopup();
	
	WPP._Menu = Frame;
end

concommand.Add("wpp_menu",WPP.Menu);

/*===========================================================================================================================
	Hook: WPP.PopulateToolMenu
	Purpose: Creates the WPP Utilities Menu.
	Author: Goluch
===========================================================================================================================*/
hook.Add("PopulateToolMenu", "WPP-SpawnMenuOpen-Client", function()
	spawnmenu.AddToolMenuOption("Utilities", "User", "WPP Client", "Wolf Prop Protection", "", "", function(Panel)
		Panel:AddControl("Button", {Text = "Show WPP Menu", Command = "wpp_menu"});
		Panel:AddControl("Label", {Text = "You can also bind a key to: 'wpp_menu'"});
		
		Panel:AddControl("Button", {Text = "Show Prop Share Menu", Command = "wpp_share"});
		Panel:AddControl("Label", {Text = "You can also bind a key to: 'wpp_share'"});
		
		//Panel:AddControl("Button", {Text = "Show Admin Menu", Command = "wpp_admin"});
		//Panel:AddControl("Label", {Text = "You can also bind a key to: 'wpp_admin'"});
		Panel:AddControl("Label", {Text = "Admin menu has been moved to the 'Admin' section of the 'Utilities' tab"});
	end);
end);